package com.zhhs.galaxyott.adapters;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.activity.HotMovieActivity;
import com.zhhs.galaxyott.activity.MoreSpecialActivity;
import com.zhhs.galaxyott.activity.SingleSpecialActivity;
import com.zhhs.galaxyott.activity.movieDetailActivity.MovieDetailActivity;
import com.zhhs.galaxyott.beans.MoreSpecialBean;
import com.zhhs.galaxyott.beans.MoreSpecialTypeBean;
import com.zhhs.galaxyott.http.api.RequestMaker;
import com.zhhs.galaxyott.httpVolley.base.ResponseListener;
import com.zhhs.galaxyott.httpVolley.response.MoreSpeciaContentlResponse;
import com.zhhs.galaxyott.utils.Logs;

public class SpecialsViewPagersAdapter extends PagerAdapter implements OnClickListener {

    private Context mContext;
    private List<View> list = new ArrayList<View>();
    private List<MoreSpecialTypeBean> listData = new ArrayList<MoreSpecialTypeBean>();
    private List<MoreSpecialBean> PagerData = new ArrayList<MoreSpecialBean>();
    private HashMap<String, List<MoreSpecialBean>> mHashMap;
    private View view;
    private ImageView special_one_bg, special_two_bg, special_three_bg, special_four_bg, special_five_bg;

    public SpecialsViewPagersAdapter(Context mContext, List<View> list, List<MoreSpecialTypeBean> listData) {
        this.mContext = mContext;
        this.list = list;
        this.listData = listData;
        mHashMap = new HashMap<>();

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(list.get(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        container.addView(list.get(position));// 实例化页卡

        loadData(position, listData.get(position).getTypeId());

        return list.get(position);
    }

    // Viewpage 初始会加载本页面与邻近的页面
    private void loadData(final int position, int typeId) {
        Logs.logD("DEBUG_DT", "THIS IS getMoreSpecialContent-->typeId:" + typeId);
        RequestMaker.getMoreSpecialContent(mContext, typeId, new ResponseListener<MoreSpeciaContentlResponse>() {

            @Override
            public void onResponse(MoreSpeciaContentlResponse result) {

                if (result.mMoreSpecialBeans == null || result.mMoreSpecialBeans.size() == 0) {
                    return;
                }

                Logs.logD("DEBUG_DT", "THIS IS getMoreSpecialContent-->" + result.mMoreSpecialBeans.size());
                if (result.errCode == 0) {

                    PagerData = result.mMoreSpecialBeans;
                    mHashMap.put(String.valueOf(position), PagerData);
                    // Logs.logD("DEBUG_DT", "THIS IS mHashMap-->" +
                    // mHashMap.get("0").size() + "position" + position);

                    initView(position, PagerData);
                }

            }

            @Override
            public void onError(VolleyError error) {

            }
        });
    }

    public void initDataView(int position) {
        loadData(position, listData.get(position).getTypeId());
    }

    private void initView(int postion, List<MoreSpecialBean> PagerData) {
        view = list.get(postion);
        special_one_bg = (ImageView) view.findViewById(R.id.special_one_bg);
        special_two_bg = (ImageView) view.findViewById(R.id.special_two_bg);
        special_three_bg = (ImageView) view.findViewById(R.id.special_three_bg);
        special_four_bg = (ImageView) view.findViewById(R.id.special_four_bg);
        special_five_bg = (ImageView) view.findViewById(R.id.special_five_bg);

        special_one_bg.setOnClickListener(this);
        special_two_bg.setOnClickListener(this);
        special_three_bg.setOnClickListener(this);
        special_four_bg.setOnClickListener(this);
        special_five_bg.setOnClickListener(this);

        showUrlImage(PagerData);

    }

    private void showUrlImage(List<MoreSpecialBean> PagerData) {

        for (int i = 0; i < PagerData.size(); i++) {
            if (i == 0) {
                special_one_bg.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(PagerData.get(i).getImage_url()).placeholder(R.drawable.morespecial_big_def)
                        .crossFade().into(special_one_bg);
            } else if (i == 1) {
                special_two_bg.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(PagerData.get(i).getImage_url())
                        .placeholder(R.drawable.morespecial_little_def).crossFade().into(special_two_bg);
            } else if (i == 2) {
                special_three_bg.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(PagerData.get(i).getImage_url())
                        .placeholder(R.drawable.morespecial_little_def).crossFade().into(special_three_bg);
            } else if (i == 3) {
                special_four_bg.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(PagerData.get(i).getImage_url())
                        .placeholder(R.drawable.morespecial_little_def).crossFade().into(special_four_bg);
            } else if (i == 4) {
                special_five_bg.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(PagerData.get(i).getImage_url())
                        .placeholder(R.drawable.morespecial_little_def).crossFade().into(special_five_bg);
            }

        }

    }

    @Override
    public boolean isViewFromObject(View v, Object object) {
        return v == object;// 官方推荐
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.special_one_bg:
            startActivity(mHashMap.get(String.valueOf(MoreSpecialActivity.pageItem)).get(0));
            break;
        case R.id.special_two_bg:
            startActivity(mHashMap.get(String.valueOf(MoreSpecialActivity.pageItem)).get(1));
            break;
        case R.id.special_three_bg:
            startActivity(mHashMap.get(String.valueOf(MoreSpecialActivity.pageItem)).get(2));
            break;
        case R.id.special_four_bg:
            startActivity(mHashMap.get(String.valueOf(MoreSpecialActivity.pageItem)).get(3));
            break;
        case R.id.special_five_bg:
            startActivity(mHashMap.get(String.valueOf(MoreSpecialActivity.pageItem)).get(4));
            break;

        default:
            break;
        }
    }

    public void startActivity(MoreSpecialBean moreSpecialBean) {
        Intent intent = new Intent();
        int type = moreSpecialBean.getContent_type();
        if (type == 1) {
            intent.setClass(mContext, MovieDetailActivity.class);
            intent.putExtra("movieid", moreSpecialBean.getObject_id());
            mContext.startActivity(intent);

        } else if (type == 2) {
            // intent.setClass(mContext, HotMovieActivity.class);
            // this.startActivity(intent);

        }
        // else if (type == 3) {
        // intent.setClass(mContext, MoreSpecialActivity.class);
        // mContext.startActivity(intent);
        //
        // }
        else if (type == 4) {
            intent.setClass(mContext, SingleSpecialActivity.class);
            intent.putExtra("specailId", moreSpecialBean.getObject_id());
            mContext.startActivity(intent);

        } else if (type == 5) {
            intent.setClass(mContext, HotMovieActivity.class);
            intent.putExtra("specailId", moreSpecialBean.getObject_id());
            mContext.startActivity(intent);

        }
    }

}
